<?php
/**
 * Created by PhpStorm.
 * User: meng
 * Date: 2023/10/11
 * Time: 15:24
 */

namespace app\admin\validator;

use lib\common\CommonValidator;
use model\AdminUserModel;

class UserValidator extends CommonValidator
{
    protected $rule = [
        'id'          => 'require|postive',
        'username'    => 'requireCallback:checkRequireUsername|max:32|usernameUnique',
        'mobile'      => 'requireCallback:checkRequireMobile|mobileNumber|mobileUnique',
        'email'       => 'requireCallback:checkRequireEmail|emailAddress|emailUnique',
        'gender'      => 'in:0,1,2',
        'avatar'      => 'max:255',
        'password'    => 'requireCallback:checkRequirePassword|length:6,16',
        'full_name'   => 'length:2,32',
        'post_name'   => 'length:2,30',
        'status'      => 'require|postive',
        'permissions' => 'require|array'
    ];

    protected $message = [
        'id.require'               => '参数中缺少用户ID',
        'id.postive'               => '用户ID只能是正整数',
        'username.requireCallback' => '用户名/手机号/邮箱至少填写其中一项',
        'username.max'             => '用户名最大长度32字符',
        'mobile.requireCallback'   => '用户名/手机号/邮箱至少填写其中一项',
        'mobile.mobileNumber'      => '手机号不正确',
        'email.requireCallback'    => '用户名/手机号/邮箱至少填写其中一项',
        'email.emailAddress'       => '邮箱地址不正确',
        'gender'                   => '性别只能选择男,女,保密',
        'avatar'                   => '头像链接超出长度限制',
        'password.length'          => '密码长度6-16字符',
        'password.requireCallback' => '密码必填',
        'full_name'                => '姓名长度2-30字符',
        'post_name'                => '岗位名称长度2-30字符',
        'status'                   => '用户状态参数不正确',
        'permissions.require'      => '请选择权限组',
        'permissions.array'        => '用户所属权限组只能是数组'
    ];

    protected $scene = [
        'create' => ['username', 'mobile', 'email', 'gender', 'avatar', 'password', 'full_name', 'post_name', 'status', 'permissions'],
        'update' => ['id', 'username', 'mobile', 'email', 'gender', 'avatar', 'password', 'full_name', 'post_name', 'status', 'permissions'],
    ];

    protected function usernameUnique ($value, $rule, $data) {
        if (!empty($value)) {
            if (!empty($data['id'])) {
                $user = make(AdminUserModel::class)->where('id', '<>', $data['id'])->where('username', $value)->find();
            } else {
                $user = make(AdminUserModel::class)->where('username', $value)->find();
            }
            if (!empty($user)) {
                return '用户名重复';
            }
        }
        return true;
    }

    protected function mobileUnique ($value, $rule, $data) {
        if (!empty($value)) {
            if (!$this->mobileNumber($value)) {
                return '手机号码不正确';
            }
            if (!empty($data['id'])) {
                $user = make(AdminUserModel::class)->where('id', '<>', $data['id'])->where('mobile', $value)->find();
            } else {
                $user = make(AdminUserModel::class)->where('mobile', $value)->find();
            }
            if (!empty($user)) {
                return '手机号码重复';
            }
        }
        return true;
    }

    protected function emailUnique ($value, $rule, $data) {
        if (!empty($value)) {
            if (!$this->emailAddress($value)) {
                return '邮箱地址不正确';
            }
            if (!empty($data['id'])) {
                $user = make(AdminUserModel::class)->where('id', '<>', $data['id'])->where('email', $value)->find();
            } else {
                $user = make(AdminUserModel::class)->where('email', $value)->find();
            }
            if (!empty($user)) {
                return '邮箱重复';
            }
        }
        return true;
    }

    protected function checkRequirePassword ($value, $data): bool {
        if (empty($data['id'])) {
            return true;
        }
        return false;
    }

    protected function checkRequireUsername ($value, $data): bool {
        if (empty($data['mobile']) && empty($data['email'])) {
            return true;
        }
        return false;
    }

    protected function checkRequireMobile ($value, $data): bool {
        if (empty($data['username']) && empty($data['email'])) {
            return true;
        }
        return false;
    }

    protected function checkRequireEmail ($value, $data): bool {
        if (empty($data['username']) && empty($data['mobile'])) {
            return true;
        }
        return false;
    }
}
